<?php
// ========================== 文件说明 ==========================//
// 本文件说明：友情链接管理
// --------------------------------------------------------------//
// 本程序作者：angel
// --------------------------------------------------------------//
// 本程序版本：SaBlog-X Ver 1.6
// --------------------------------------------------------------//
// 本程序主页：http://www.sablog.net
// ========================== 开发环境 ==========================//
// register_globals = Off
// --------------------------------------------------------------//
// magic_quotes_gpc = On
// --------------------------------------------------------------//
// safe_mode = On
// --------------------------------------------------------------//
// Windows server 2003 & Linux & FreeBSD
// --------------------------------------------------------------//
// Apache/1.3.33 & PHP/4.3.2 & MySQL/4.0.17
// --------------------------------------------------------------//
// Apache/1.3.34 & PHP/4.4.1 & MySQL/5.0.16
// --------------------------------------------------------------//
// Apache/2.0.55 & PHP/5.1.1 & MySQL/5.0.15
// --------------------------------------------------------------//
// Copyright (C) Security Angel Team All Rights Reserved.
// ==============================================================//

if(!defined('SABLOG_ROOT') || !isset($php_self) || !preg_match("/[\/\\\\]cp\.php$/", $php_self)) {
	exit('Access Denied');
}

//权限检查
permission();

//批量处理
if($_POST['action'] == 'domorelink') {
	if($ids = implode_ids($_POST['delete'])) {
		$DB->query("DELETE FROM	{$db_prefix}links WHERE linkid IN ($ids)");
	}
	if(is_array($_POST['name'])) {
		foreach($_POST['name'] as $linkid => $value) {
			$DB->unbuffered_query("UPDATE {$db_prefix}links SET displayorder='".intval($_POST['displayorder'][$linkid])."', name='".char_cv(trim($_POST['name'][$linkid]))."', url='".char_cv(trim($_POST['url'][$linkid]))."', note='".char_cv(trim($_POST['note'][$linkid]))."', visible='".intval($_POST['visible'][$linkid])."' WHERE linkid='".intval($linkid)."'");
		}
	}
	$new_displayorder = intval($_POST['new_displayorder']);
	$new_visible = intval($_POST['new_visible']);
	$new_name = char_cv(trim($_POST['new_name']));
	$new_url = char_cv(trim($_POST['new_url']));
	$new_note = char_cv(trim($_POST['new_note']));

	if($new_name != '') {
		$DB->query("INSERT INTO	{$db_prefix}links (displayorder,name,url,note,visible) VALUES ('$new_displayorder','$new_name','$new_url','$new_note','$new_visible')");
	}
	links_recache();
    redirect('链接已成功更新', 'cp.php?job=link');
}

!$action && $action = 'list';

if ($action == 'list') {
	$sql_query = ' WHERE 1=1 ';
	$subnav = '全部链接';
	$view = in_array($_GET['view'], array('display','hidden')) ? $_GET['view'] : '';
	if ($view == 'display') {
		$sql_query .= " AND visible='1'";
		$pagelink   = '&view=display';
		$subnav     = '全部显示的链接';
	}
	if ($view == 'hidden') {
		$sql_query .= " AND visible='0'";
		$pagelink   = '&view=hidden';
		$subnav     = '全部隐藏的链接';
	}

	if($page) {
		$start_limit = ($page - 1) * 20;
	} else {
		$start_limit = 0;
		$page = 1;
	}
	$total = $DB->result($DB->query("SELECT COUNT(*) FROM {$db_prefix}links $sql_query"), 0);
	$multipage = multi($total, 20, $page, 'cp.php?job=link&action=list'.$pagelink);

	$query = $DB->query("SELECT * FROM {$db_prefix}links $sql_query ORDER BY displayorder LIMIT $start_limit, 20");
	$linkdb = array();
	while ($link = $DB->fetch_array($query)) {
		$link['visible_check'] = $link['visible'] ? 'checked' : '';
		$linkdb[] = $link;
	}
	unset($link);
	$DB->free_result($query);
	$subnav = '链接管理';
}

$navlink_L = ' &raquo; <a href="cp.php?job=link">链接管理</a>'.($subnav ? ' &raquo; '.$subnav : '');
cpheader($subnav);
include template('link');
?>